home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / gnu / WinGnuPlot.lha / WinGnuPlot / Source / PlotImage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  6.1 KB  |  257 lines

  1. /* this implements the Plot BOOPSI Gagdet */
  2.  
  3. #include "struct.c"
  4. #include "WinPlot.h"
  5. #include "OutlineFont.h"
  6. #include "amigawin.h"
  7.  
  8. #define NUMCOLORS 10
  9.  
  10. struct MUI_Palette_Entry ColorEntries[] =
  11. {
  12.  {  0, 0x00000000, 0x00000000, 0x00000000,  0}, /* border */
  13.  {  1, 0x22222222, 0x22222222, 0x22222222,  1}, /* axes */
  14.  {  2, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,  2}, /* background */
  15.  {  3, 0x00000000, 0x00000000, 0x00000000,  6},
  16.  {  4, 0x33333333, 0x33333333, 0x33333333,  7},
  17.  {  5, 0x77777777, 0x77777777, 0x77777777,  8},
  18.  {  6, 0xBBBBBBBB, 0xBBBBBBBB, 0xBBBBBBBB,  9},
  19.  {  7, 0xFFFFFFFF, 0x00000000, 0x00000000,  3},
  20.  {  8, 0x00000000, 0xFFFFFFFF, 0x00000000,  4},
  21.  {  9, 0x00000000, 0x00000000, 0xFFFFFFFF,  5},
  22.  { MUIV_Palette_Entry_End,0,0,0,0 },
  23. };
  24.  
  25. char *ColorNames[] =
  26. {
  27.  "Border",
  28.  "Axes",
  29.  "BackGround",
  30.  "Color 0",
  31.  "Color 1",
  32.  "Color 2",
  33.  "Color 3",
  34.  "Color 4",
  35.  "Color 5",
  36.  "Color 6",
  37. };
  38.  
  39. Class *PlotImgClass = NULL;
  40.  
  41. struct PlotImageData
  42. {
  43.  struct ColorMap *cm;
  44.  UWORD            Pens[NUMCOLORS];
  45. };
  46.  
  47. static ULONG __asm DispatchPlotImage(register __a0 Class *, 
  48.                      register __a2 Object *,
  49.                      register __a1 Msg);
  50.  
  51. int _STI_10000_InitPlotImgClass(void)
  52. {                                
  53.  Class *cl;
  54.  
  55.  if (cl = MakeClass(NULL, GADGETCLASS, NULL, sizeof(struct PlotImageData), 0))
  56.   {
  57.    cl->cl_Dispatcher.h_Entry = (ULONG(*)()) DispatchPlotImage;
  58.    PlotImgClass = cl;
  59.  
  60.    return 0;
  61.   }
  62.  
  63.  return 1;
  64. }
  65.  
  66. void _STD_10000_FreePlotImgClass(void)
  67. {
  68.  if (PlotImgClass) FreeClass(PlotImgClass);
  69. }
  70.  
  71. /* a quick&dirty emulation of OS3 ObtainBestPen */
  72. static int GetPen(struct ColorMap *cm, UBYTE Depth, ULONG lr, ULONG lg, ULONG lb)
  73. {
  74.  ULONG entrie;
  75.  ULONG  sum, best;
  76.  BYTE  r1, g1, b1;
  77.  UBYTE r, g, b;
  78.  int   i, bestpen;
  79.  
  80.  r = lr >> 28; g = lg >> 28; b = lb >> 28;
  81.  best = INT_MAX; bestpen = 0;
  82.  for(i = 0; i<Depth; i++)
  83.   {
  84.    entrie = GetRGB4(cm, i);
  85.    r1 = (entrie >> 16) & 0x0F;
  86.    g1 = (entrie >>  8) & 0x0F;
  87.    b1 = entrie & 0x0F;
  88.  
  89.    sum = (r1-r)*(r1-r)+(g1-g)*(g1-g)+(b1-b)*(b1-b);
  90.    if (sum < best)
  91.     {
  92.      best = sum;
  93.      bestpen = i;
  94.     }
  95.   }
  96.  
  97.  return bestpen;
  98. }
  99.  
  100. struct PlotCommand *commands = NULL;
  101. int                 ncommands = 0;
  102.  
  103. /* this the main plot routine, which interprets the GnuPlot draw-commands */
  104. static void __interrupt RenderImage(struct gpRender *gpr, struct Gadget *g,
  105.                     struct PlotImageData *pig)
  106. {
  107.  int              i, w, h;
  108.  int              justify = 0;
  109.  int              x,y,xmin,xmax,ymin,ymax,txlen; 
  110.  BOOL             Vertical = FALSE;
  111.  struct Task     *MyTask; 
  112.  struct RastPort *rp = gpr->gpr_RPort;
  113.  
  114.  MyTask = FindTask(NULL);
  115.  if (MyTask->tc_Node.ln_Type == NT_PROCESS)
  116.   {
  117.    if (GfxBase->LibNode.lib_Version >= 39)
  118.     {
  119.      if (pig->cm != NULL)
  120.       for(i=0; i<NUMCOLORS; i++)
  121.        ReleasePen(pig->cm, pig->Pens[i]);
  122.      else
  123.       pig->cm = gpr->gpr_GInfo->gi_Screen->ViewPort.ColorMap;
  124.  
  125.      for(i=0; i<NUMCOLORS; i++)
  126.       pig->Pens[i] = ObtainBestPen(pig->cm,
  127.                    ColorEntries[i].mpe_Red,
  128.                    ColorEntries[i].mpe_Green,
  129.                    ColorEntries[i].mpe_Blue,
  130.                    OBP_Precision, PRECISION_IMAGE,
  131.                    TAG_END);
  132.     }
  133.    else
  134.     {
  135.      UBYTE Depth = 1 << gpr->gpr_GInfo->gi_DrInfo->dri_Depth;
  136.  
  137.      pig->cm = gpr->gpr_GInfo->gi_Screen->ViewPort.ColorMap;
  138.      for(i=0; i<NUMCOLORS; i++)
  139.       pig->Pens[i] = GetPen(pig->cm, Depth,
  140.                 ColorEntries[i].mpe_Red,
  141.                 ColorEntries[i].mpe_Green,
  142.                 ColorEntries[i].mpe_Blue);
  143.     }
  144.    SetAPen(rp, pig->Pens[2]);
  145.    RectFill(rp, g->LeftEdge, g->TopEdge,
  146.         g->LeftEdge+g->Width-1, g->TopEdge+g->Height-1);
  147.    SetAPen(rp, pig->Pens[0]);
  148.    SetDrMd(rp, JAM1);
  149.    
  150.    w = g->Width;
  151.    h = g->Height;
  152.    SetFontSize(w, h);
  153.    for(i=0; i<ncommands; i++)
  154.     switch (commands[i].pc_com)
  155.      {
  156.      case LINETYP:
  157.       if (commands[i].pc_arg1 == -2)
  158.        SetAPen(rp, pig->Pens[0]);
  159.       else if (commands[i].pc_arg1 == -1)
  160.        SetAPen(rp, pig->Pens[1]);
  161.       else 
  162.        SetAPen(rp, pig->Pens[3+ commands[i].pc_arg1 % (NUMCOLORS-3)]);
  163.       break;
  164.      case JUSTIFY:
  165.       justify = commands[i].pc_arg1;
  166.       break;
  167.      case TEXT_ANGLE:
  168.       Vertical = commands[i].pc_arg1 == 1;
  169.       break;
  170.      case PUT_TEXT:
  171.       txlen = TextLen(commands[i].pc_argstr);
  172.       x = (commands[i].pc_arg1 * w) / RESOLUTION;
  173.       y = h-(commands[i].pc_arg2 * h) / RESOLUTION;
  174.       if (!Vertical)
  175.        {
  176.     switch (justify)
  177.      {
  178.      case 0 :
  179.       xmin = x;
  180.       xmax = x + txlen;
  181.       break;
  182.      case 1:
  183.       xmin = x - txlen/2;
  184.       xmax = x + txlen/2;
  185.       break;
  186.      case 2:
  187.       xmin = x - txlen;
  188.       xmax = x;
  189.       break;
  190.      }
  191.     ymin = y;
  192.     ymax = ymin+h/70;
  193.     PrintString(rp, g->LeftEdge+xmin, g->TopEdge+ ymax,
  194.             commands[i].pc_argstr, FALSE); 
  195.        }
  196.       else
  197.        {
  198.     ymax = y + txlen/2;
  199.     PrintString(rp, g->LeftEdge+x, g->TopEdge+ymax, commands[i].pc_argstr, TRUE);
  200.        }
  201.       break;
  202.      case MOVE:
  203.       Move(rp,
  204.        g->LeftEdge + (commands[i].pc_arg1 * w) / RESOLUTION,
  205.        g->TopEdge + h - (commands[i].pc_arg2 * h) / RESOLUTION);
  206.       break;
  207.      case VECTOR:
  208.       Draw(rp,
  209.        g->LeftEdge + (commands[i].pc_arg1 * w) / RESOLUTION,
  210.        g->TopEdge + h - (commands[i].pc_arg2 * h) / RESOLUTION);
  211.       break;
  212.      }
  213.   }
  214. }
  215.  
  216. /* the BOOPSI stuff, quite standard */
  217. /* Although this is a BOOPSI-Gadget, we only need its image, */
  218. /* so the input related stuff ist not implemented */
  219. static ULONG __interrupt __asm __saveds DispatchPlotImage(register __a0 Class *cl,
  220.                               register __a2 Object *o,
  221.                               register __a1 Msg msg)
  222. {
  223.  ULONG                 retval = FALSE;
  224.  int                   i;
  225.  Object               *object;
  226.  struct PlotImageData *this;
  227.  
  228.  switch (msg->MethodID)
  229.   {
  230.   case GM_RENDER:
  231.    this = INST_DATA(cl, o);
  232.    RenderImage((struct gpRender *)msg, (struct Gadget *)o, this);
  233.    retval = TRUE;
  234.    break;
  235.   case OM_NEW:
  236.    if (object = (Object *)DoSuperMethodA(cl, o, msg))
  237.     {
  238.      this = INST_DATA(cl, object);
  239.      this->cm = NULL;
  240.     }
  241.    retval = (ULONG)object;
  242.    break;
  243.   case OM_DISPOSE:
  244.    this = INST_DATA(cl, o);
  245.    if (GfxBase->LibNode.lib_Version >= 39 && this->cm)
  246.     for(i=0; i<NUMCOLORS; i++)
  247.      ReleasePen(this->cm, this->Pens[i]);
  248.    retval = TRUE;
  249.    break;
  250.   default:
  251.    retval = DoSuperMethodA(cl, o, msg);
  252.    break;
  253.   }
  254.  
  255.  return retval;
  256. }
  257.